package cn.tyoui.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tyoui.bean.DdBean;
import cn.tyoui.bean.HgBean;
import cn.tyoui.bean.PriceBean;
import cn.tyoui.bean.UserBean;
import cn.tyoui.bean.XyffBean;
import cn.tyoui.bean.YfzlBean;
import cn.tyoui.bean.cfddBean;
import cn.tyoui.mapping.DdManageMapper;
import cn.tyoui.service.CfddManageService;
import cn.tyoui.service.HgManageService;
import cn.tyoui.service.PriceManageService;
import cn.tyoui.service.XyffManageService;
import cn.tyoui.service.YfzlManageService;

@Controller
public class Wyxd {

	@Resource
	private CfddManageService cfddManageService;

	@Resource
	private YfzlManageService yfzlManageService;

	@Resource
	private XyffManageService xyffManageService;

	@Resource
	private PriceManageService priceManageService;

	@Resource
	private DdManageMapper ddManageMapper;
	
	@Resource
	private HgManageService hgManageService;

	
	@ResponseBody
	@RequestMapping("qu_pj")
	public Map<String, Object> qu_pj(@RequestBody DdBean DdBean) {
		System.out.println("qu_pj");
		System.out.println(DdBean);
		Map<String, Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		if(DdBean.getId() != 0) {
			try {
				isSuccess = ddManageMapper.qu_pj(DdBean);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("admin_change_ddzt")
	public Map<String, Object> admin_change_ddzt(@RequestBody DdBean DdBean) {
		System.out.println("admin_change_ddzt");
		System.out.println(DdBean);
		Map<String, Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		if(DdBean.getId() != 0) {
			try {
				isSuccess = ddManageMapper.admin_change_ddzt(DdBean);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("admin_change_jd")
	public Map<String, Object> admin_change_jd(@RequestBody Map<String, String> map) {
		System.out.println(map);
		Map<String, Object> resultMap = new HashMap<>();
		String id = map.get("id");
		boolean isSuccess = false;
		if(!id.equals("")) {
			try {
				isSuccess = ddManageMapper.admin_change_jd(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("query_all_dd")
	public Map<String, Object> query_all_dd(@RequestBody Map<String, String> map) {
		System.out.println(map);
		Map<String, Object> resultMap = new HashMap<>();
		List<DdBean> ddList = new ArrayList<>();
		try {
			ddList = ddManageMapper.query_all_dd(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("ddList : " + ddList.size());
		if(ddList.size() != 0) {
			System.out.println(ddList.get(0));
		}
		resultMap.put("ddList", ddList);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("user_delete_wddd")
	public Map<String, Object> user_delete_wddd(@RequestBody DdBean DdBean) {
		System.out.println("user_delete_wddd");
		System.out.println(DdBean);
		Map<String, Object> resultMap = new HashMap<>();
		HgBean hgBean = new HgBean();
		hgBean.setCfdd(DdBean.getCfdd());
		hgBean.setHghm(DdBean.getHghm());
		hgBean.setUse("δʹ��");
		boolean isSuccess = false;
		try {
			if(hgManageService.changeHgState(hgBean)) {
				ddManageMapper.deleteDd(String.valueOf(DdBean.getId()));
				isSuccess = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("query_wddd")
	public Map<String, Object> queryWddd(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		HttpSession session = request.getSession();
		UserBean user = (UserBean) session.getAttribute("user");
		String id = String.valueOf(user.getId());
		List<DdBean> ddList = new ArrayList<>();
		try {
			ddList = ddManageMapper.queryWddd(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("ddList : " + ddList.size());
		resultMap.put("ddList", ddList);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("wyxd_query")
	public Map<String, Object> wyxdQuery() {
		Map<String, Object> resultMap = new HashMap<>();
		cfddBean cfddBean = new cfddBean();
		cfddBean.setCfdd("");
		List<cfddBean> cfddList = new ArrayList<>();
		List<YfzlBean> yfzlList = new ArrayList<>();
		List<XyffBean> xyffList = new ArrayList<>();
		try {
			cfddList = cfddManageService.queryCfdd(cfddBean);
			resultMap.put("cfdd", cfddList);
			YfzlBean yfzlBean = new YfzlBean();
			yfzlBean.setYfzl("");
			yfzlList = yfzlManageService.queryYfzl(yfzlBean);
			resultMap.put("yfzl", yfzlList);
			XyffBean xyffBean = new XyffBean();
			xyffBean.setXyff("");
			xyffList = xyffManageService.queryXyff(xyffBean);
			resultMap.put("xyff", xyffList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(cfddList.size());
		System.out.println(yfzlList.size());
		System.out.println(xyffList.size());
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("js_price")
	public Map<String, Object> jsPrice(@RequestBody PriceBean PriceBean) {
		Map<String, Object> resultMap = new HashMap<>();
		List<PriceBean> list = new ArrayList<>();
		;
		try {
			list = priceManageService.queryOnePrice(PriceBean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultMap.put("jiage", list);
		System.out.println(list.size());
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("wyxd")
	public Map<String, Object> wyxd(@RequestBody DdBean DdBean,HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		System.out.println(DdBean);
		HttpSession session = request.getSession();
		UserBean user = (UserBean) session.getAttribute("user");
		
		String ddmx = DdBean.getDdmx();
		String ddmx1 = ddmx.substring(0, ddmx.length()-1);
		System.out.println(ddmx1);
		String[] ddmxArr = ddmx1.split(",");
		System.out.println("ddmx.length : "+ddmxArr.length);
		int zj = 0;
		String yfzl = "";
		String xyff = "";
		int zyfsl = 0;
		for(int i = 0;i < ddmxArr.length;i++) {
			String a = ddmxArr[i];
			String[] aa = a.split(";");
			System.out.println("aa.length :" + aa.length);
			PriceBean priceBean = new PriceBean();
			int yfsl = 0;
			for(int j = 0;j < aa.length;j++) {
				if(j == 0) {
					priceBean.setYfzl(aa[0]);
					yfzl += aa[0]+',';
				}
				if(j == 1) {
					priceBean.setXyff(aa[1]);
					xyff += aa[1] + ',';
				}
				if(j == 2) {
					zyfsl += Integer.parseInt(aa[2]);
					yfsl = Integer.parseInt(aa[2]);
				}
			}
			try {
				List<PriceBean> priceBean1 =priceManageService.queryOnePrice(priceBean);
				zj += Integer.parseInt(priceBean1.get(0).getPrice())* yfsl;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("yfzl : " + yfzl);
		System.out.println("xyff : " + xyff);
		System.out.println("yfsl : " + zyfsl);
		System.out.println("zj  :  "  + zj);
		DdBean.setUserId(String.valueOf(user.getId()));
		DdBean.setUserName(user.getUserName());
		List<PriceBean> list = new ArrayList<>();
		PriceBean p = new PriceBean();
		p.setXyff(DdBean.getXyff());
		p.setYfzl(DdBean.getYfzl());
		try {
			HgBean hgBean = new HgBean();
			hgBean.setCfdd(DdBean.getCfdd());
			hgBean.setUse("δʹ��");
			System.out.println(hgBean);
			List<HgBean> hgs = hgManageService.queryOneHg(hgBean);
			System.out.println("hgs : " + hgs.size());
			if(hgs.size() > 0) {
				DdBean.setSfjd("δ�ӵ�");
				DdBean.setHghm(hgs.get(0).getHghm());
				DdBean.setDdzt("");
				DdBean.setPj("");
				DdBean.setYfzl(yfzl);
				DdBean.setYfsl(zyfsl);
				DdBean.setXyff(xyff);
				DdBean.setZj(zj);
				ddManageMapper.addDd(DdBean);
				resultMap.put("isSuccess", true);
				resultMap.put("msg", "�µ��ɹ� �� ����ص���("+DdBean.getCfdd() + "--->"+ hgs.get(0).getHghm()+"�Ż���)");
				HgBean hgBean1 = new HgBean();
				hgBean1 = hgs.get(0);
				hgBean1.setUse("��ʹ��");
				hgManageService.changeHg(hgBean1);
				
			}else {
				resultMap.put("isSuccess", false);
				resultMap.put("msg", "�ô�ŵص������,������ѡ��");
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultMap.put("jiage", list);
		System.out.println(list.size());
		return resultMap;
	}
}
